<?php
/**
 * 会员中心
 * User: chen guang
 * Date: 2017/9/29 9:00
 *
 */

namespace app\admin\controller;
use app\admin\controller\Export;
use think\Db;

class Membership extends Common{

	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();

	}

	/**
	 * 会员列表
	 */
	public function list_index(){
		$status=input("status");
		$where = [];
		$whereor = [];
		if($status != ''){
			$where['ma.status']=$status;
		}
		if(input("keyword")){
			$where['m.nickname']=array("like",'%'.input('keyword').'%');
			$whereor['ma.mobile']=array("like",'%'.input('keyword').'%');
		}

		if(request()->isAjax()){
			$status=input("data_status");
			$id=input("id");
			$result = db('member_apply')->where(['id'=>$id])->update(['status'=>$status]);
			if(!$result)$this->error('修改失败');
			$this->success('修改成功');
		}
		
		$list = db("member_apply")->alias('ma')->join('member m','m.id=ma.uid')->where($where)->whereor($whereor)->order("ma.id desc")->field('ma.*,m.sex,m.nickname,m.headimage')->paginate(10);
		$count = db("member_apply")->alias('ma')->join('member m','m.id=ma.uid')->where($where)->whereor($whereor)->order("ma.id desc")->count();
		$this->assign('list',$list);
		$this->assign('count',$count);
		$this->assign('status',input("status"));
		$this->assign('keyword',input("keyword"));
		return $this->fetch();
	}

	/**
	* 提现申请
	*/
	public function list_withdrawals(){
		$where=array();
        $status=input('status');
        if($status!=''){
            $where['w.status'] =$status;
        }
        $keyword=input('keyword');
        if($keyword!=''){
            $where['w.bank|m.nickname|w.name|w.mobile'] =array("like",'%'.$keyword.'%');
        }
        $list = db("member_withdrawals")->alias("w")
            ->field("w.*,m.nickname")
            ->join("member m","m.id=w.uid")
            ->where($where)
            ->order("w.id desc")
            ->paginate(10);

        $count =  db("member_withdrawals")->alias("w")
            ->join("member m","m.id=w.uid")
            ->where($where)
            ->count();
        $this->assign("list",$list);
        $this->assign("count",$count);
        $this->assign("status",$status);
        $this->assign("keyword",$keyword);

		return $this->fetch();
	}

	/**
	* 平台回扣
	*/
	public function list_system_balance(){
		$order_sn = input('order_sn');

		$total_money = db('system_balance')->sum('money');

		$where = [];
		if(!empty($order_sn)){
			$where['o.order_sn'] = $order_sn;
		}

		$list = db("system_balance")->alias('sb')->join('order o','o.id=sb.order_id')->where($where)->field('sb.*,o.order_sn')->paginate(10);

		$this->assign('total_money',$total_money);
		$this->assign('order_sn',$order_sn);
		$this->assign('list',$list);
		return $this->fetch();
	}

	/**
	* 佣金回扣
	*/
	public function list_member_balance(){
		$order_sn = input('order_sn');
		$mobile = input('mobile');
		$nickname = input('nickname');

		$where = [];

		if(!empty($nickname)){
			$where['m.nickname'] = ['like','%'.$nickname.'%'];
		}

		if(!empty($order_sn)){
			$where['o.order_sn'] = $order_sn;
		}

		if(!empty($mobile)){
			$where['ma.mobile'] = $mobile;
		}

		$total_money = db('member_balance')->sum('money');

		$list = db("member_balance")->alias('sb')->join('order o','o.id=sb.order_id')->join('member m','m.id=sb.uid')->join('shop s','s.id=sb.shop_id')->join('member_apply ma','ma.uid=sb.uid')->where($where)->field('sb.*,o.order_sn,m.headimage,m.nickname,s.shopname,ma.mobile')->paginate(10);

		$this->assign('total_money',$total_money);
		$this->assign('nickname',$nickname);
		$this->assign('order_sn',$order_sn);
		$this->assign('mobile',$mobile);
		$this->assign('list',$list);
		return $this->fetch();
	}

	/*
	 * 司机提现
	 */
	public function list_driver_withdrawals()
	{
		$where=array();
		$status=input('status');
		if($status!=''){
			$where['w.status'] =$status;
		}
		$keyword=input('keyword');
		if($keyword!=''){
			$where['w.bank|m.nickname|w.name|w.mobile'] =array("like",'%'.$keyword.'%');
		}
		$list = db("driver_withdrawals")->alias("w")
				->field("w.*,m.nickname")
				->join("driver m","m.id=w.driver_id")
				->where($where)
				->order("w.id desc")
				->paginate(10);

		$count =  db("driver_withdrawals")->alias("w")
				->join("driver m","m.id=w.driver_id")
				->where($where)
				->count();
		$this->assign("list",$list);
		$this->assign("count",$count);
		$this->assign("status",$status);
		$this->assign("keyword",$keyword);

		return $this->fetch();
	}

	/*
	 * 司机收入列表
	 */
	public function list_driver_balance()
	{
		$order_sn = input('order_sn');
		$mobile = input('mobile');
		$nickname = input('nickname');

		$where = [];

		if(!empty($nickname)){
			$where['m.nickname'] = ['like','%'.$nickname.'%'];
		}

		if(!empty($order_sn)){
			$where['o.order_sn'] = $order_sn;
		}

		if(!empty($mobile)){
			$where['ma.mobile'] = $mobile;
		}

		$total_money = db('driver_balance')->sum('money');

		$list = db("driver_balance")->alias('sb')->join('order o','o.id=sb.order_id')->join('driver m','m.id=sb.driver_id')->join('shop s','s.id=sb.shop_id')->where($where)->field('sb.*,o.order_sn,m.headimage,m.nickname,s.shopname,m.mobile')->paginate(10);
//var_dump($list);
		$this->assign('total_money',$total_money);
		$this->assign('nickname',$nickname);
		$this->assign('order_sn',$order_sn);
		$this->assign('mobile',$mobile);
		$this->assign('list',$list);
		return $this->fetch();
	}


	/**
	* 自提佣金
	*/
	public function list_mention_balance(){
		$order_sn = input('order_sn');

		$total_money = db('mention_balance')->sum('money');

		$where = [];
		if(!empty($order_sn)){
			$where['o.order_sn'] = $order_sn;
		}

		$list = db("mention_balance")->alias('sb')->join('order o','o.id=sb.order_id')->where($where)->field('sb.*,o.order_sn')->paginate(10);

		$this->assign('total_money',$total_money);
		$this->assign('order_sn',$order_sn);
		$this->assign('list',$list);
		return $this->fetch();
	}
}